package com.push.notifications;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.push.cache.ImageLoader;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class popupNews extends Activity{
	public static float scale= 1;
	
	public static String titleDialog;
	public static String descriptonDialg;
	public static String urlImage;
	public static String sortContentDialog;
	public static String sCommand;
	public static String sUrl;
	public static boolean bLink;
	public static boolean bAdmin;
	
	public ImageLoader imageLoaderDialog;
	public static Handler han_popupNews;
	
	private boolean bFinal= false;
	

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		 setContentView(R.layout.message_old);
		 GCMIntentService.news_islive = true;
		 imageLoaderDialog=new ImageLoader(this.getApplicationContext());
		
		ImageButton buttonClose1 = (ImageButton) findViewById(R.id.imageButton12);
		buttonClose1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//close Activitys
                finish();
            }
        });
        han_popupNews = new Handler() {

            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch(msg.what) {

                case 0:
                	finish();
                    break;

                }
            }

        };
		TextView TextZinZin = (TextView) findViewById(R.id.TittleReceive);
		TextZinZin.setText(Definitions.TITLE_DIALOG);
		
		TextView TextTitleMessage = (TextView) findViewById(R.id.textView1);
		TextTitleMessage.setText(titleDialog);
		
		TextView TextDescript = (TextView) findViewById(R.id.textView2);
		TextDescript.setText(descriptonDialg);
		
		TextView TextMessage = (TextView) findViewById(R.id.textView3);
		
		TextMessage.setText(Html.fromHtml(sortContentDialog +"..."+ "<font color=#F36523><i>" + Definitions.READ_MORE + "</i></font>"));	
		TextView Message = (TextView) findViewById(R.id.textView3);
		
		RelativeLayout contentLayout = (RelativeLayout)findViewById(R.id.webView1);
		
		ImageView ImageMessage = (ImageView) findViewById(R.id.imageView1);
		imageLoaderDialog.DisplayImage(urlImage, ImageMessage);
				
		contentLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                  	
            	if(bAdmin == true ) //Get data from push_data on server
            	{
            		if(popupNews.bLink == false) //view internal web
            		{
            			MessageActivity.messageCataloge = descriptonDialg;
            			MessageActivity.title = titleDialog;
            			MessageActivity.url= popupNews.sUrl ;
            			Intent intent_MessageFull = new Intent(popupNews.this, MessageActivity.class);
            			startActivity(intent_MessageFull);	
            			finish();
            		}
            		else
            		{
	            		startContent();		            		
            		}
            	}
            	else if(sCommand == null || (sCommand !=null &&sCommand.equalsIgnoreCase("title")))
            	{
            		if(popupNews.bLink == false) //view internal web
            		{
            			MessageActivity.messageCataloge = descriptonDialg;
            			MessageActivity.title = titleDialog;
            			MessageActivity.url= popupNews.sUrl ;
            			Intent intent_MessageFull = new Intent(popupNews.this, MessageActivity.class);
            			
            			startActivity(intent_MessageFull);	
            			finish();
            		}    		        
    		        else // view by browser
    		        {
    		        	startContent();
    		        	
    		        }
    		        
            	}
            	
            }
        });

			
	}
	
	public void startContent()
	{
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(popupNews.sUrl));
    	try {
    		startActivity(browserIntent);
    		finish();
        } catch (ActivityNotFoundException anfe) {

        	//show dialog alert
        }
	}
	@Override
	protected void onStop() {
		Log.e("popupNews", "onStop: ------onStop------------ "+GCMIntentService.AppInterrupt);
		super.onStop();
	}
	  @Override
  	public void onWindowFocusChanged(boolean hasFocus) {
	  if (hasFocus)
	  {
		  GCMIntentService.AppInterrupt =false;
	  }
	// TODO Auto-generated method stub
	  Log.e("popupNews", ": ------onWindowFocusChanged------------ onWindowFocusChanged-"+ GCMIntentService.AppInterrupt);
	super.onWindowFocusChanged(hasFocus);
  	}
	@Override
	protected void 	onUserLeaveHint(){
		//GCMIntentService.AppInterrupt =true;
		Log.e("popupNews", "onUserLeaveHint: ------onUserLeaveHint------------ "+ GCMIntentService.AppInterrupt);
		super.onUserLeaveHint();
	}
	@Override
	public void finish() {
		GCMIntentService.news_islive= false;
		Log.e("popupNews", "finish: ------finish------------ "+ GCMIntentService.AppInterrupt);
		// TODO Auto-generated method stub
		super.finish();
	}

	
	
}
